<?
//link den smarty class
require_once('include/sessionclass.php');
session_start();
require_once('libs/Smarty.class.php');
require_once('include/oracle.php');
require_once('include/validation_class.php');
require_once('include/config_color.php');
require_once('include/ngonngu.php');
require_once('include/login.php');
require_once('include/reader.php');
require_once('include/myfunction.php');
//$USERID ='00000';
$smarty = new Smarty();
/********/
$TCVN=array('¸','µ','¶','·','¹','¨','¾','»','¼','½','Æ', '©','Ê','Ç','È','É','Ë', '®','Ð','Ì','Î','Ï','Ñ', 'ª','Õ','Ò','Ó','Ô','Ö', 
'Ý','×','Ø','Ü','Þ', 'ã','ß','á','â','ä', '«','è','å','æ','ç','é', '¬','í','ê','ë','ì','î', 'ó','ï','ñ','ò','ô', '­','ø','õ','ö','÷','ù', 
'ý','ú','û','ü','þ', '¡','¢','§','£','¤','¥','¦');

/****************************************************/
$smarty->template_dir = 'smarty/templates';
$smarty->compile_dir = 'smarty/templates_c';
$smarty->cache_dir = 'smarty/cache';
$smarty->config_dir = 'smarty/configs';
/**************************************************************************/
$smarty->assign('lbl_FILE', $VN_readexel[NAME]);

$smarty->assign('upload', $VN_readexel[UPLOAD]);
$smarty->assign('sendftp', $VN_readexel[SENDFTP]);
$smarty->assign('readexel', $VN_readexel[READ]);
//=========================================================================
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$javascript ='<link href="style/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/checknumber.js"></script>
<script language="JavaScript" type="text/javascript" src="js/tongquat.js"></script>
<META HTTP-EQUIV="Refresh" CONTENT="' . $GLOBALS['conf_time']*60 .';URL=http://' . $host . $uri . '/blank.php?log=logout">
';
$smarty->assign('javascript', $javascript);
//=========================================================================
//==========================================================================
if(isset($_POST['sendftp'])){
	$path = './FILEFTP/';
	$FILENAME = $_FILES['FILENAME'];
	$name = $path.$FILENAME['name'];	
	//$tmp_name_ftp = rand(111,99999999999);
	$error = uploadfile($FILENAME);
	if($error=='FILS' || $error=='FILEX'){
		//=========chuyen tu exel sang text============
		$data = new Spreadsheet_Excel_Reader();
		$data->setOutputEncoding('CP1251');
		$data->read($name);
		
		$tmpname = split_string($FILENAME['name'],'.');
		$nametext = $path.$tmpname[0].".txt";
		$strquery = "$USERID|".$tmpname[0].".txt"."|*EOF";
		
		$name_ftp = $tmpname[0].".txt";
		$fp = fopen($nametext, 'w');
		error_reporting(E_ALL ^ E_NOTICE);
		$checkwrite=1;
		for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
			$result = '';
			$tmp = '';
			for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
				$result = $result.$data->sheets[0]['cells'][$i][$j]."|";
			}
			$rs = fwrite($fp, "$result\r\n");
			if(!$rs){
				$checkwrite=0;
			}
		}
		fclose($fp);
		//=============ket thuc text====================	
		//=============chuyen sang xml==================
	/*	$xml = new DOMDocument();
		$id = $xml->createElement("par_ssrate");
		$xml->appendChild($id);
		$data = new Spreadsheet_Excel_Reader();
		$data->setOutputEncoding('CP1251');
		$data->read($name);
		error_reporting(E_ALL ^ E_NOTICE);
		for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
			$infomation = $xml->createElement("infomation");
			$id->appendChild($infomation);
			$BNKID = $xml->createAttribute("BNKID");
			$infomation->appendChild($BNKID);
			$BRNCODE = $xml->createElement("BRNCODE");
			$infomation->appendChild($BRNCODE);
			$CODE = $xml->createElement("CODE");
			$infomation->appendChild($CODE);
			$NAMES = $xml->createElement("NAMES");
			$infomation->appendChild($NAMES);
			$RATE = $xml->createElement("RATE");
			$infomation->appendChild($RATE);
			$EFFDATE = $xml->createElement("EFFDATE");
			$infomation->appendChild($EFFDATE);
			$MAXRATE = $xml->createElement("MAXRATE");
			$infomation->appendChild($MAXRATE);
			$MINRATE = $xml->createElement("MINRATE");
			$infomation->appendChild($MINRATE);
			
			$v_BNKID = $xml->createTextNode($data->sheets[0]['cells'][$i][1]);
			$BNKID->appendChild($v_BNKID);
			$v_BRNCODE = $xml->createTextNode($data->sheets[0]['cells'][$i][2]);
			$BRNCODE->appendChild($v_BRNCODE);
			$v_CODE = $xml->createTextNode($data->sheets[0]['cells'][$i][3]);
			$CODE->appendChild($v_CODE);
			$v_NAMES = $xml->createTextNode($data->sheets[0]['cells'][$i][4]);
			$NAMES->appendChild($v_NAMES);
			$v_RATE = $xml->createTextNode($data->sheets[0]['cells'][$i][5]);
			$RATE->appendChild($v_RATE);
			$v_EFFDATE = $xml->createTextNode($data->sheets[0]['cells'][$i][6]);
			$EFFDATE->appendChild($v_EFFDATE);
			$v_MAXRATE = $xml->createTextNode($data->sheets[0]['cells'][$i][7]);
			$MAXRATE->appendChild($v_MAXRATE);
			$v_MINRATE = $xml->createTextNode($data->sheets[0]['cells'][$i][8]);
			$MINRATE->appendChild($v_MINRATE);
		}
		$xml->save($path.$tmpname[0].'.xml');*/
		//=============ket thuc xml=====================
		
		if($checkwrite==1){
			$error = sendtoftp($nametext,$name_ftp);
			if($error == 'SENS'){
				$MDLNAME = "IP88888_Importexcel";
				$p_msg = '';
				$name = $MDLNAME;
				$p_msg = $strquery;
				//echo "$p_msg<br>";
				$conn = openconnection();
				$query = "begin $name(:p_msg); end;";
				$stmt =oci_parse($conn,$query) or die ('Can not parse query');
				oci_bind_by_name($stmt,":p_msg",$p_msg,strlen($p_msg)+50);
				oci_execute($stmt);
				oci_close($conn);
				//echo $p_msg;
				$result = split_string($p_msg,"|");	
				if($result[0]=='*ERR'){// Error				
					$err_par = $result[1];				
					$err_field = $result[2];				
					$err_result = get_procedure_3(pro_getrspcode,$err_par);
					if($row = oci_fetch_array ($err_result,OCI_NUM)){
						$error = str_replace($TCVN,$UNICODE,$row[2]);
					}
					$javascriptsetstyle = '<script language="javascript" type="text/javascript">		
												document.getElementById(\''. $err_field .'\').style.background
		=\'' . $err_color . '\'</script>';
					//echo htmlspecialchars($javascriptsetstyle);
					$smarty->assign('javascriptsetstyle',$javascriptsetstyle);	
				}elseif($result[0]=='*RET'){
					if($result[1]=='CM001'){
						$error = $VN_template[FAILD];
						$smarty->assign('error_exel', $error);
					}else{
						//Thanh cong;
						$error = $VN_template[SUCCESS];		
						$smarty->assign('error_exel', $error);	
					}
				}
			}else{
				$smarty->assign('error_exel', $VN_readexel[$error]);
			}
		}else{
			$smarty->assign('error_exel', $VN_readexel[STRF]);
		}
	}else{
		$smarty->assign('error_exel', $VN_readexel[$error]);
	}
}
//==========================================================================
if(isset($_POST['readexel'])){
	$path = './FILEFTP/';
	$FILENAME = $_FILES['FILENAME'];
	$error = uploadfile($FILENAME);
	//echo $error;
	if($error=='FILS' || $error=='FILEX'){
		$name = $path.$FILENAME['name'];
		//=================================
		
		$allow_url_override = 1; // Set to 0 to not allow changed VIA POST or GET
		if(!$allow_url_override || !isset($file_to_include))
		{
			$file_to_include = $name;
		}
		if(!$allow_url_override || !isset($max_rows))
		{
			$max_rows = 0; //USE 0 for no max
		}
		if(!$allow_url_override || !isset($max_cols))
		{
			$max_cols = 5; //USE 0 for no max
		}
		if(!$allow_url_override || !isset($debug))
		{
			$debug = 0;  //1 for on 0 for off
		}
		if(!$allow_url_override || !isset($force_nobr))
		{
			$force_nobr = 1;  //Force the info in cells not to wrap unless stated explicitly (newline)
		}
		$data = new Spreadsheet_Excel_Reader();
		$data->setOutputEncoding('CPa25a');
		$data->read($file_to_include);
		
		error_reporting(E_ALL ^ E_NOTICE);
		$value = '';
		$value = $value."
		<STYLE>
		.table_data
		{
			border-style:ridge;
			border-width:1;
		}
		.tab_base
		{
			background:#C5D0DD;
			font-weight:bold;
			border-style:ridge;
			border-width:1;
			cursor:pointer;
		}
		.table_sub_heading
		{
			background:#CCCCCC;
			font-weight:bold;
			border-style:ridge;
			border-width:1;
		}
		.table_body
		{
			background:#F0F0F0;
			font-wieght:normal;
			font-size:12;
			font-family:sans-serif;
			border-style:ridge;
			border-width:1;
			border-spacing: 0px;
			border-collapse: collapse;
		}
		.tab_loaded
		{
			background:#222222;
			color:white;
			font-weight:bold;
			border-style:groove;
			border-width:1;
			cursor:pointer;
		}
		</STYLE>
		";
		function make_alpha_from_numbers($number)
		{
			$numeric = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			if($number<strlen($numeric))
			{
				return $numeric[$number];
			}
			else
			{
				$dev_by = floor($number/strlen($numeric));
				return "" . make_alpha_from_numbers($dev_by-1) . make_alpha_from_numbers($number-($dev_by*strlen($numeric)));
			}
		}
		$value = $value."<SCRIPT LANGUAGE='JAVASCRIPT'>
		var sheet_HTML = Array();\n";
		for($sheet=0;$sheet<count($data->sheets);$sheet++)
		{
			$table_output[$sheet] .= "<TABLE CLASS='table_body'>
			<TR>
				<TD>&nbsp;</TD>";
			for($i=0;$i<$data->sheets[$sheet]['numCols']&&($i<=$max_cols||$max_cols==0);$i++)
			{
				$table_output[$sheet] .= "<TD CLASS='table_sub_heading' ALIGN=CENTER>" . make_alpha_from_numbers($i) . "</TD>";
			}
			for($row=1;$row<=$data->sheets[$sheet]['numRows']&&($row<=$max_rows||$max_rows==0);$row++)
			{
				$table_output[$sheet] .= "<TR><TD CLASS='table_sub_heading'>" . $row . "</TD>";
				for($col=1;$col<=$data->sheets[$sheet]['numCols']&&($col<=$max_cols||$max_cols==0);$col++)
				{
					if($data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'] >=1 && $data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'] >=1)
					{
						$this_cell_colspan = " COLSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];
						$this_cell_rowspan = " ROWSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];
						for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];$i++)
						{
							$data->sheets[$sheet]['cellsInfo'][$row][$col+$i]['dontprint']=1;
						}
						for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];$i++)
						{
							for($j=0;$j<$data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];$j++)
							{
								$data->sheets[$sheet]['cellsInfo'][$row+$i][$col+$j]['dontprint']=1;
							}
						}
					}
					else if($data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'] >=1)
					{
						$this_cell_colspan = " COLSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];
						$this_cell_rowspan = "";
						for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['colspan'];$i++)
						{
							$data->sheets[$sheet]['cellsInfo'][$row][$col+$i]['dontprint']=1;
						}
					}
					else if($data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'] >=1)
					{
						$this_cell_colspan = "";
						$this_cell_rowspan = " ROWSPAN=" . $data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];
						for($i=1;$i<$data->sheets[$sheet]['cellsInfo'][$row][$col]['rowspan'];$i++)
						{
							$data->sheets[$sheet]['cellsInfo'][$row+$i][$col]['dontprint']=1;
						}
					}
					else
					{
						$this_cell_colspan = "";
						$this_cell_rowspan = "";
					}
					if(!($data->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']))
					{
						$table_output[$sheet] .= "<TD CLASS='table_data' $this_cell_colspan $this_cell_rowspan>&nbsp;";
						if($force_nobr)
						{
							$table_output[$sheet] .= "<NOBR>";
						}
						$table_output[$sheet] .= nl2br(htmlentities($data->sheets[$sheet]['cells'][$row][$col]));
						if($force_nobr)
						{
							$table_output[$sheet] .= "</NOBR>";
						}
						$table_output[$sheet] .= "</TD>";
					}
				}
				$table_output[$sheet] .= "</TR>";
			}
			$table_output[$sheet] .= "</TABLE>";
			$table_output[$sheet] = str_replace("\n","",$table_output[$sheet]);
			$table_output[$sheet] = str_replace("\r","",$table_output[$sheet]);
			$table_output[$sheet] = str_replace("\t"," ",$table_output[$sheet]);
			if($debug)
			{
				$debug_output = print_r($data->sheets[$sheet],true);
				$debug_output = str_replace("\n","\\n",$debug_output);
				$debug_output = str_replace("\r","\\r",$debug_output);
				$table_output[$sheet] .= "<PRE>$debug_output</PRE>";
			}
			$value = $value."sheet_HTML[$sheet] = \"$table_output[$sheet]\";\n";
		}
		$value = $value."
		function change_tabs(sheet){
			for(i=0;i<".count($data->sheets).";i++){
				document.getElementById('sheet_tab_' + i).className = 'tab_base';
			}
			document.getElementById('table_loader_div').innerHTML=sheet_HTML[sheet];
			document.getElementById('sheet_tab_' + sheet).className = 'tab_loaded';
		
		}
		</SCRIPT>";
		$value = $value."
		<TABLE CLASS='table_body' NAME='tab_table'>
		<TR>";
		for($sheet=0;$sheet<count($data->sheets);$sheet++)
		{
			$value = $value."<TD CLASS='tab_base' ID='sheet_tab_$sheet' ALIGN=CENTER
				ONMOUSEDOWN=\"change_tabs($sheet);\">".$data->boundsheets[$sheet]['name']."</TD>";
		}
		
		$value = $value."<TR>";
		$value = $value."</TABLE>
		<DIV ID=table_loader_div></DIV>
		<SCRIPT LANGUAGE='JavaScript'>
		change_tabs(0);
		</SCRIPT>";
		
		//=================================
		$value=$value."</table>";
		$value = str_replace($TCVN,$UNICODE,$value);
		$smarty->assign('value', $value);
	}else{
		//echo "123";
		$smarty->assign('error_exel', $VN_readexel[$error]);
	}
}
//==============================================
	
$smarty->display('read_exel.tpl');
?>